import {
	roleListApi,
	roleFuncsApi,
} from '@utils/apis'
import {
	roleAdd,
	roleEdit,
	roleDel,
	roleFuncs,
	roleGrant,
	submit,
	cancel,
} from './role-methods'

module.exports = function initUserPage(user) {
	$._store = {}
	let comps = {
		formDialog: $("#role-form-dialog"),
		dataGrid: $("#main-tabs-role"),
		roleForm: $("#role-form"),
		roleId: $('#role-id'),
		roleName: $('#role-name'),
		roleDesc: $('#role-desc'),
		formBtnSave: $("#role-form-btn-save"),
		formBtnCancel: $("#role-form-btn-cancel"),
		roleFuncDlg: $('#role-func-dialog'),
		roleFunc: $('#role-func'),
		rfBtnSave: $('#role-func-btn-save'),
		rfBtnCancel: $('#role-func-btn-cancel'),
	}
	// let funcs = user.funcMap['role_management']
	initDataGrid(comps, user)
	initEditDailog(comps, user)
	initRoleFuncs(comps, user)
}

function initDataGrid(comps, user) {
	let {
		dataGrid,
		formDialog,
	} = comps
	let options = {
		fit: true,
		fitColumns: true,
		rownumbers: true,
		singleSelect: true,
		toolbar: [],
		columns: [
			[{
				field: 'role_id',
				title: '角色ID',
				width: 80
			}, {
				field: 'role_name',
				title: '角色名',
				width: 80
			}, {
				field: 'desc',
				title: '描述',
				width: 120,
			}, {
				field: 'create_time',
				title: '创建时间',
				width: 80,
			}]
		],
	}
	// 添加用户
	if (user.func_ids.indexOf('role_add') > -1) {
		options.toolbar.push({
			iconCls: 'icon-add',
			text: '新建',
			handler() {
				roleAdd(comps, user, 'role_add')
			}
		}, '-')
	}
	// 修改角色信息
	if (user.func_ids.indexOf('role_modify') > -1) {
		options.toolbar.push({
			iconCls: 'icon-edit',
			text: '修改',
			handler() {
				roleEdit(comps, user, 'role_modify')
			}
		}, '-')
	}
	// 删除角色
	if (user.func_ids.indexOf('role_del') > -1) {
		options.toolbar.push({
			iconCls: 'icon-remove',
			text: '删除',
			handler() {
				roleDel(comps, user, 'role_del')
			}
		}, '-')
	}
	// 角色授权
	if (user.func_ids.indexOf('role_funcs') > -1) {
		options.toolbar.push({
			iconCls: 'icon-lock',
			text: '授权',
			handler() {
				roleGrant(comps, user, 'role_funcs')
			}
		})
	}
	// 角色列表
	if (user.func_ids.indexOf('role_list') > -1) {
		options.url = user.funcMap['role_list'].func_url
	}
	dataGrid.datagrid(options)
}

function initEditDailog(comps, user) {
	let {
		formBtnSave,
		formBtnCancel,
	} = comps
	formBtnSave.linkbutton({
		text: '保存',
		iconCls: 'icon-save',
		onClick() {
			submit(comps, user)
		},
	})
	formBtnCancel.linkbutton({
		text: '取消',
		iconCls: 'icon-cancel',
		onClick() {
			cancel(comps.formDialog, comps.roleForm)
		},
	})
}

function initRoleFuncs(comps, user) {
	let {
		roleFuncDlg,
		roleFunc,
		rfBtnSave,
		rfBtnCancel,
		dataGrid,
	} = comps
	roleFunc.treegrid({
		data: [],
		animate: true,
		lines: true,
		checkbox: true,
		fitColumns: true,
		rownumbers: true,
		idField: 'id',
		treeField: 'name',
		columns: [
			[{
				field: 'name',
				title: '功能名称',
				width: 180,
			}, {
				field: 'func_id',
				title: '功能ID',
				width: 180,
			}, {
				field: 'func_url',
				title: '功能链接',
				width: 180,
			}, {
				field: 'desc',
				title: '功能描述',
				width: 120
			}]
		],
		onBeforeCheckNode(row, checked) {
			console.log('row, checked', row, checked)
			if (!$._store.grant)
				return true
			let selectedUser = dataGrid.datagrid('getSelected')
			if (selectedUser.role_id == 'admin') {
				$.messager.alert('警告', '管理员用户不能授权！', 'warning');
				return false
			}
			return true
		},
	})
	rfBtnSave.linkbutton({
		text: '保存',
		iconCls: 'icon-save',
		onClick() {
			roleFuncs(comps, user)
		},
	})
	rfBtnCancel.linkbutton({
		text: '取消',
		iconCls: 'icon-cancel',
		onClick() {
			cancel(roleFuncDlg)
		},
	})
	// roleFuncsApi().then(res => {
	// 	if (res.code != '000')
	// 		return
	// 	let {
	// 		menuTree,
	// 		moduleFuncMap,
	// 	} = res.result
	// 	let data = getFuncTreeData(menuTree, moduleFuncMap)
	// 	roleFunc.treegrid('loadData', data)
	// })
}

function getFuncTreeData(menuTree, moduleFuncMap) {
	let treeData = []
	for (let i = 0; i < menuTree.length; i++) {
		let module = menuTree[i]
		let data = {
			id: module.module_id,
			name: module.module_name,
			func_id: '',
			func_url: '',
			desc: '',
		}
		if (Array.isArray(module.children))
			data.children = getFuncTreeData(module.children, moduleFuncMap)
		if (module.route) {
			let func_ids = Object.keys(moduleFuncMap[module.module_id])
			data.children = []
			for (let i = 0; i < func_ids.length; i++) {
				let func_id = func_ids[i]
				let func = moduleFuncMap[module.module_id][func_id]
				data.children.push({
					id: func.func_id,
					name: func.func_name,
					func_id: func.func_id,
					func_url: func.func_url,
					desc: func.desc,
				})
			}
		}
		treeData.push(data)
	}
	return treeData
}